//
//  SYResult.swift
//  Entrance
//
//  Created by chunhuiLai on 2018/1/10.
//  Copyright © 2018年 chunhuiLai. All rights reserved.
//

import Foundation
import SwiftyJSON

protocol SYSuccessProtocol {
    var data: JSON {get set}
    var returnMsg: String? {get set}
}

protocol SYFailureProtocol {
    var statusCode: Int? {get set}
    var returnCode: Int? {get set}
    var data: JSON {get set}
    var returnMsg: String {get set}
    
    var dealCode: SYReturnCode? {get set}
}

struct SYSuccess: SYSuccessProtocol {
    var data: JSON = JSON("")
    var returnMsg: String?
}

struct SYFailure: SYFailureProtocol {
    
    var statusCode: Int?
    var returnCode: Int?
    var data: JSON = JSON("")
    var returnMsg: String = SYReturnCode.App.noNetwork.rawValue
    
    var dealCode: SYReturnCode?
}

enum SYResult<T: SYSuccessProtocol, E: SYFailureProtocol> {
    
    case success(T)
    case failure(E)
    
    public init(value: T) {
        self = .success(value)
    }
    
    public init(value: E) {
        self = .failure(value)
    }
}

struct SYReturnCode {
    
    enum Server: Int {
        case normal = 1
        case stopServer = -999
        case AdjustTime = -4
        case loginOutByForce = 4
    }
    
    enum App: String {
        case noNetwork = "网络不给力,请稍后再试"       /// 网络异常
        case serverError = "哎呀,加载出错了~"     /// 服务器异常
        case encryptError = "哎呀,加载出错了!"    /// 解密错误
    }
    
    var server: Server?
    var app: App?
}

struct SYStatusCode {
    
    enum Server: Int {
        case overRequest = 299
    }
    
    var server: Server
}
